package com.zc.framework.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.HashMap;

/**
 * Created by zc on 2016/11/30.
 */
public class SessionUtil implements Serializable {

    private static final long serialVersionUID = -5270805355157290011L;

    private HashMap sessionValue = new HashMap();

    public SessionUtil() {

    }

    public static void setAttribute(HttpServletRequest request, String subsystem, String module, String page, String key, Object value) {
        if(request != null) {
            SessionUtil tmp = getMGRObject(request);
            tmp.setAttribute(subsystem, module, page, key, value);
            request.getSession().setAttribute("SYSTEM_SESSION_MGR", tmp);
        }
    }

    public static void setAttribute(HttpServletRequest request, String subsystem, String key, Object value) {
        if(request != null) {
            SessionUtil tmp = getMGRObject(request);
            tmp.setAttribute(subsystem, "", "", key, value);
            request.getSession().setAttribute("SYSTEM_SESSION_MGR", tmp);
        }
    }

    public static void setAttribute(HttpServletRequest request, String subsystem, String module, String key, Object value) {
        if(request != null) {
            SessionUtil tmp = getMGRObject(request);
            tmp.setAttribute(subsystem, module, "", key, value);
            request.getSession().setAttribute("SYSTEM_SESSION_MGR", tmp);
        }
    }



    public static void setAttribute(HttpServletRequest request, String key, Object value) {
        if(request != null) {
            SessionUtil tmp = getMGRObject(request);
            tmp.setAttribute("", "", "", key, value);
            request.getSession().setAttribute("SYSTEM_SESSION_MGR", tmp);
        }
    }

    private void setAttribute(String subsystem, String module, String page, String key, Object value) {

        if(subsystem != null && subsystem.length() > 0) {
            HashMap subsystemMap1 = (HashMap)this.sessionValue.get(subsystem);
            if(subsystemMap1 == null) {
                subsystemMap1 = new HashMap();
                this.sessionValue.put(subsystem, subsystemMap1);
            }

            if(module != null && module.length() > 0) {
                HashMap moduleMap1 = (HashMap)subsystemMap1.get(module);
                if(moduleMap1 == null) {
                    moduleMap1 = new HashMap();
                    subsystemMap1.put(module, moduleMap1);
                }

                if(page != null && page.length() > 0) {
                    HashMap pageMap1 = (HashMap)moduleMap1.get(page);
                    if(pageMap1 == null) {
                        pageMap1 = new HashMap();
                        moduleMap1.put(page, pageMap1);
                    }

                    pageMap1.put(key, value);
                } else {
                    moduleMap1.put(key, value);
                }
            } else {
                subsystemMap1.put(key, value);
            }
        } else {
            this.sessionValue.put(key, value);
        }

    }

    public static Object getAttribute(HttpServletRequest request, String key) {
        if(request == null) {
            return null;
        } else {
            SessionUtil tmp = getMGRObject(request);
            return tmp.getAttribute("", "", "", key);
        }
    }

    public static Object getAttribute(HttpSession session, String key) {
        if(session == null) {
            return null;
        } else {
            SessionUtil tmp = getMGRObject(session);
            return tmp.getAttribute("", "", "", key);
        }
    }

    public static Object getAttribute(HttpServletRequest request, String subsystem, String key) {
        if(request == null) {
            return null;
        } else {
            SessionUtil tmp = getMGRObject(request);
            return tmp.getAttribute(subsystem, "", "", key);
        }
    }

    public static Object getAttribute(HttpServletRequest request, String subsystem, String module, String key) {
        if(request == null) {
            return null;
        } else {
            SessionUtil tmp = getMGRObject(request);
            return tmp.getAttribute(subsystem, module, "", key);
        }
    }

    public static Object getAttribute(HttpServletRequest request, String subsystem, String module, String page, String key) {
        if(request == null) {
            return null;
        } else {
            SessionUtil tmp = getMGRObject(request);
            return tmp.getAttribute(subsystem, module, page, key);
        }
    }

    private Object getAttribute(String subsystem, String module, String page, String key) {
        Object attribute;
        if(subsystem != null && subsystem.length() > 0) {
            HashMap tmp = (HashMap)this.sessionValue.get(subsystem);
            if(tmp == null) {
                return null;
            }

            if(module != null && module.length() > 0) {
                if(tmp.get(module) == null) {
                    return null;
                }

                tmp = (HashMap)tmp.get(module);
                if(page != null && page.length() > 0) {
                    if(tmp.get(page) == null) {
                        return null;
                    }

                    tmp = (HashMap)tmp.get(page);
                    attribute = tmp.get(key);
                } else {
                    attribute = tmp.get(key);
                }
            } else {
                attribute = tmp.get(key);
            }
        } else {
            attribute = this.sessionValue.get(key);
        }

        return attribute;
    }


    private static SessionUtil getMGRObject(HttpServletRequest request) {
        HttpSession tmpSession = request.getSession(false);
        if(tmpSession == null) {
            return new SessionUtil();
        } else {
            SessionUtil tmp = (SessionUtil)tmpSession.getAttribute("SYSTEM_SESSION_MGR");
            if(tmp == null) {
                tmp = new SessionUtil();
            }

            return tmp;
        }
    }

    private static SessionUtil getMGRObject(HttpSession tmpSession) {
        SessionUtil tmp = (SessionUtil)tmpSession.getAttribute("SYSTEM_SESSION_MGR");
        if(tmp == null) {
            tmp = new SessionUtil();
        }

        return tmp;
    }
}
